package cn.gailvlun.gll.presentation.login;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;

import com.github.markzhai.recyclerview.BaseViewAdapter;

import java.util.List;

import cn.gailvlun.gll.R;
import cn.gailvlun.gll.databinding.ActivitySelectHeaderBinding;
import cn.gailvlun.gll.net.HttpMethods;
import cn.gailvlun.gll.net.HttpSubscriber;
import cn.gailvlun.gll.net.base.GetAvatarRes;
import cn.gailvlun.gll.presentation.base.BaseActivity;
import cn.gailvlun.gll.presentation.login.adapter.SelectHeaderAdapter;
import cn.gailvlun.gll.util.IsEmpty;
import cn.gailvlun.gll.util.RxUtil;
import cn.gailvlun.gll.util.ToastUtil;
import cn.gailvlun.gll.widget.NavigationBarView;
import io.reactivex.Observable;
import rx_activity_result2.Result;
import rx_activity_result2.RxActivityResult;



public class SelectHeaderActivity extends BaseActivity {

    public static void openActivity(Context context) {
        Intent intent = new Intent(context, SelectHeaderActivity.class);
        context.startActivity(intent);
    }

    public static Observable<Result<Activity>> openActivityForResult(Activity activity) {
        Intent intent = new Intent(activity, SelectHeaderActivity.class);
        return RxActivityResult.on(activity)
                .startIntent(intent);
    }

    private ActivitySelectHeaderBinding mBinding;

    private SelectHeaderAdapter mAdapter;
    private GridLayoutManager mLayoutManager;

    @Override
    protected void initComponent() {
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_select_header);

        mAdapter = new SelectHeaderAdapter(mContext);
        mAdapter.setPresenter(new Presenter());
        mBinding.recyclerView.setAdapter(mAdapter);

        mLayoutManager = new GridLayoutManager(mContext, 3);
        mBinding.recyclerView.setLayoutManager(mLayoutManager);
    }

    @Override
    protected void createHandler() {
        mBinding.nbv.setOnBackListener(new NavigationBarView.OnBackListener() {
            @Override
            public void onBack(NavigationBarView nb) {
                onBackPressed();
            }
        });
    }

    @Override
    protected void loadData(@Nullable Bundle savedInstanceState) {
        HttpMethods.getLoginService()
                .getAvatar()
                .compose(RxUtil.<List<GetAvatarRes>>applyScheduler())
                .subscribe(new HttpSubscriber<List<GetAvatarRes>>() {
                    @Override
                    protected void onSuccess(List<GetAvatarRes> response) {
                        if (!IsEmpty.list(response)) {
                            mAdapter.addAll(response);
                        }
                    }

                    @Override
                    protected void onFailure(String errMsg, @Nullable List<GetAvatarRes> response, int code) {
                        ToastUtil.showShort(errMsg);
                    }
                });
    }

    public class Presenter implements BaseViewAdapter.Presenter {
        public void onChooseAvatar(GetAvatarRes getAvatarRes) {
            Intent intent = new Intent();
            intent.putExtra("data", getAvatarRes);
            setResult(RESULT_OK, intent);
            finish();
        }
    }
}
